<?php
/*
 * course控制器  打开主页位置
 * Tobey
 * */
require_once "baseController.class.php";
class courseController extends baseController{
	/**
	 * GET请求  打开课程管理页面
	 * ------------------------------------------------------------------------------------
	 * ajax请求  获取某个教师的所有课程   
	 * 			 参数np 当前页数
	 */
	public function course(){
		if(IS_GET) {
			$this->display("Course/course");
		} else if(IS_AJAX && IS_LOCAL) {
            session_start();
            if($_SESSION["isTeacher"] == "true"){
                if (isset($_POST["np"]) && is_numeric($_POST["np"])) {
                    $p = $_POST["np"];
                } else {
                    $p = 1;
                }
			    $sql = D();				//开启数据库连接
                $where["teacher_tea_id"] = $_SESSION["id"];
                $table = array("course"=>"");
			     $d["pageInfo"] = $sql->table($table)->where($where)->page($p,20);	//获取分页情况
			    //获取课程信息
                 $d["course"] = $sql->table($table)->where($where)->field(array("cou_id", "cou_name", "cou_code", "is_available"))->	order("cou_id desc")->limit(($d["pageInfo"]["np"]-1)*$d["pageInfo"]["num"], $d["pageInfo"]["num"])->select();
                 $data = $this->message(1, "获取信息成功", $d);
                 unset($d,$sql,$where,$table);
             } else{
                $data = $this->message(0,"你没有权限对此操作!","");
             }
             $this->ajaxReturn($data);
             unset($data);
              session_write_close();
         }
     }

	/**
	 * 获取可用课程的基本信息  (用于筛选班级)
	 * ajax
	 */
	public function getEnableCourse(){
		if(IS_AJAX && IS_LOCAL) {
			session_start();
			$this->ajaxReturn($this->message(1,"获取信息成功",D()->table(array("course"=>""))->where(array("teacher_tea_id"=>$_SESSION["id"],"is_available"=>1))->field(array("cou_id", "cou_name"))->select()));
			session_write_close();
		}
	}

	/**
	 * 新增课程  
	 * ajax
	 */
	public function add(){
		if(IS_AJAX && IS_LOCAL){
			session_start();
            $_POST["teacher_tea_id"] = $_SESSION["id"];     //绑定老师id
            $table = array("course"=>"");
            $sql = D();
            if($sql->table($table)->check($_POST,1)){			//自动验证
            	 //检查是否有同名的课程存在			
            	if($sql->table($table)->where(array("teacher_tea_id"=>$_SESSION["id"],"cou_name"=>$_POST["cou_name"]))->count() == 0){ 
                    $d["id"] = $sql->table($table)->add($_POST); 
                    if($d["id"]){
                        $d["cou_name"] = $_POST["cou_name"];
                        $data = $this->message(1,"新增课程成功!",$d);
                    } else{
                       $data = $this->message(0,"新增课程失败!","");
                   }
                   unset($d);
               } else{
                  $data = $this->message(0,"该课程已经存在,请重新改名!","");
              }
          } else{
             $data = $this->message(0,$sql->getCheckError(),"");
         }
         $this->ajaxReturn($data);
         unset($sql,$data,$table);
         session_write_close();
     }
 }

    /**
     * 检测能否删除课程
     * ajax   cou_id  课程id
     */
    public function check_delete(){
    	if(IS_AJAX && IS_LOCAL) {
            $data = $this->coursePower();        //检测权限
            if($data["status"] == 1) {             //如果返回1表示有权限去操作课程
                //检查课程下面是否存在班级   有班级则不能删除
            	if(D()->table(array("class"=>""))->where(array("cou_id"=>$_POST["cou_id"]))->count() == 0){
            		$data = $this->message(1,"该课程可以直接删除!","");
            	} else{
            		$data = $this->message(0,"该课程下有班级,不能直接删除!","");
            	}
            }
            $this->ajaxReturn($data);       //返回数据
            unset($data);
        }
    }

    /**
     * 删除课程
     * ajax  cou_id
     */
    public function delete() {
    	if(IS_AJAX && IS_LOCAL){
            $data = $this->coursePower();          //检查权限是否足够
            if($data["status"] == 1) {
            	if(D()->table(array("course"=>""))->where(array("cou_id"=>$_POST["cou_id"]))->delete()){
            		$data = $this->message(1,"课程删除成功!","");
            	} else{
            		$data = $this->message(0,"课程删除失败!","");
            	}
            }
            $this->ajaxReturn($data);
            unset($data);
        }
    }

    /**
     * 修改课程
     * ajax   cou_id  cou_name  is_available  cou_code(可有可无)
     */
    public function update(){
    	if(IS_AJAX && IS_LOCAL && isset($_POST["cou_id"]) && is_numeric($_POST["cou_id"])){

            $data = $this->coursePower();      //检查权限
            if($data["status"] == 1) {
            	$table = array("course"=>"");
            	$sql = D();
                $sql->startTrans();			//开启事务管理
                if($sql->table($table)->check($_POST,2)){
                //若要修改课程为不可以   必须该课程下的所有班级都要归档了才可以  否则无效
                	if($_POST["is_available"] == 0 && $sql->table(array("class"=>""))->where(array("cou_id"=>$_POST["cou_id"],"is_filed"=>0))->count() > 0){
                		$_POST["is_available"] = 1;
                	}
                	if($sql->table($table)->where(array("cou_id"=>$_POST["cou_id"]))->update($_POST)){		//更新信息
                		//检查是否存在2个同名的课程
                        session_start();
                        if($sql->table($table)->where(array("teacher_tea_id"=>$_SESSION["id"],"cou_name"=>$_POST["cou_name"]))->count() <= 1){
                           $sql->commit();
                           $data = $this->message(1,"课程修改成功!","");
                       } else{
                			$sql->rollback();		//回滚
                			$data = $this->message(0,"同名课程已经存在,请重新改名!","");
                		}
                        session_write_close();
                    } else{
                      $sql->rollback();
                      $data = $this->message(0,"课程信息没有任何变化,无需修改!","");	
                  }
              } else{
                 $data = $this->message(0,$sql->getCheckError(),"");
             }
             unset($table,$sql);
         }
         $this->ajaxReturn($data);
         unset($data);
     }
 }

  	/**
  	 * 析构函数不用写任何内容也是可以的
  	 */
  	public function __destruct(){}
  }